package com.cjh.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cjh.pojo.Menu;
import com.cjh.pojo.Order;
import com.cjh.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index() {
        return "order-8010的端口号是：" + port;
    }

    @PostMapping("/insertOrder")
    public boolean insertOrder(@RequestBody Order order) {
        System.out.println("Order订单:" + order);
        return orderService.insertOrder(order);
    }

    @GetMapping("/selectOrdersByUserId/{userId}")
    public PageInfo<Order> selectOrdersByUserId(@PathVariable("userId") Integer userId,
                                                @RequestParam("page") Integer page,
                                                @RequestParam("limit") Integer limit) {

        PageHelper.startPage(page, limit);
        List<Order> orders = orderService.selectOrdersByUserId(userId);
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        return pageInfo;
    }

    @GetMapping("/selectUntreatedOrders")
    public PageInfo<Order> selectUntreatedOrders(@RequestParam("page") Integer page,
                                                 @RequestParam("limit") Integer limit) {

        PageHelper.startPage(page, limit);
        List<Order> orders = orderService.selectUntreatedOrders();
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        return pageInfo;
    }

    @PutMapping("/updateOrderState/{adminId}")
    public boolean updateOrderState(@PathVariable("adminId") Integer adminId, @RequestParam("id") Integer id) {
        //update t_order set aid=1,state=1 where id=26
        UpdateWrapper updateWrapper = new UpdateWrapper();
        //将订单改成已派送状态
        updateWrapper.set("state", 1);
        //设置管理员
        updateWrapper.set("aid", adminId);
        updateWrapper.eq("id", id);

        return orderService.update(updateWrapper);
    }

    @DeleteMapping("/deleteOrder")
    public boolean deleteOrder(@RequestParam("id") Integer id){

        return orderService.removeById(id);
    }

    @GetMapping("/getOrderNumber/{userId}")
    public Integer getOrderNumber(@PathVariable Integer userId) {

        return orderService.getOrderNumber(userId);
    }
}
